{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "08cee08a-72af-4850-b9fe-771cc2af66b8",
   "metadata": {},
   "source": [
    "Chapter 07\n",
    "# 四个for循环例子\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "30b8b70a-184e-4774-859d-737e54744804",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "M\n",
      "a\n",
      "t\n",
      "p\n",
      "l\n",
      "o\n",
      "t\n",
      "l\n",
      "i\n",
      "b\n"
     ]
    }
   ],
   "source": [
    "# 循环字符串内字符\n",
    "\n",
    "str_for_loop = 'Matplotlib'\n",
    "\n",
    "for str_idx in str_for_loop:\n",
    "    print(str_idx)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "56da2cd6-305d-4be1-a3ff-392611398cc2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matplotlib\n",
      "NumPy\n",
      "Seaborn\n",
      "Pandas\n",
      "Plotly\n",
      "SK-learn\n"
     ]
    }
   ],
   "source": [
    "# 循环list中元素\n",
    "list_for_loop = ['Matplotlib', 'NumPy', 'Seaborn', \n",
    "                 'Pandas', 'Plotly', 'SK-learn']\n",
    "for item_idx in list_for_loop:\n",
    "    print(item_idx)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "584e9ef2-e94f-41a9-be26-31a6b8923319",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=================\n",
      "Matplotlib\n",
      "A visualization tool\n",
      "=================\n",
      "NumPy\n",
      "=================\n",
      "Seaborn\n",
      "A visualization tool\n",
      "=================\n",
      "Pandas\n",
      "=================\n",
      "Plotly\n",
      "A visualization tool\n",
      "=================\n",
      "SK-learn\n"
     ]
    }
   ],
   "source": [
    "# 循环中嵌入 if 判断\n",
    "packages_visual = ['Matplotlib', 'Seaborn', 'Plotly']\n",
    "for item_idx in list_for_loop:\n",
    "    print('=================')\n",
    "    print(item_idx)\n",
    "    if item_idx in packages_visual:\n",
    "        print('A visualization tool')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "248d28e5-06fb-4481-a55c-563374a5fd70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===============\n",
      "Matplotlib\n",
      "M\n",
      "a\n",
      "t\n",
      "p\n",
      "l\n",
      "o\n",
      "t\n",
      "l\n",
      "i\n",
      "b\n",
      "===============\n",
      "NumPy\n",
      "N\n",
      "u\n",
      "m\n",
      "P\n",
      "y\n",
      "===============\n",
      "Seaborn\n",
      "S\n",
      "e\n",
      "a\n",
      "b\n",
      "o\n",
      "r\n",
      "n\n",
      "===============\n",
      "Pandas\n",
      "P\n",
      "a\n",
      "n\n",
      "d\n",
      "a\n",
      "s\n",
      "===============\n",
      "Plotly\n",
      "P\n",
      "l\n",
      "o\n",
      "t\n",
      "l\n",
      "y\n",
      "===============\n",
      "SK-learn\n",
      "S\n",
      "K\n",
      "-\n",
      "l\n",
      "e\n",
      "a\n",
      "r\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "# 嵌套 for 循环\n",
    "for item_idx in list_for_loop:\n",
    "    print('===============')\n",
    "    print(item_idx)\n",
    "    \n",
    "    for item_idx in item_idx:\n",
    "        print(item_idx)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
